package com.weichat.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.model.Event_user;
import com.model.Point;
import com.model.Propertr_user;
import com.model.RunRecord;
import com.model.User;
import com.service.Event_userService;
import com.service.Propertr_userService;
import com.service.RunService;

@RestController
public class RunController {

	@Autowired
	RunService runservice;

	@Autowired
	Event_userService event_userService;

	@Autowired
	Propertr_userService propertr_userService;

	@PostMapping("startRun")
	public int startRun(HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("user");

		RunRecord runRecord = new RunRecord();
		runRecord.setUserId(user.getId());
		runservice.insertSelective(runRecord);

		Event_user event_user = new Event_user();
		event_user.setEventId(8);
		event_user.setUserId(user.getId());
		event_user.setType("R");
		event_userService.insertSelective(event_user);
		System.out.println(runRecord.getId());
		return runRecord.getId();
	}

	@PostMapping("endRun")
	public void endRun(HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("user");

		Event_user event_user = event_userService.selectEidByUidOrder(user.getId());
		event_user.setType("Y");
		event_userService.updateByPrimaryKeySelective(event_user);

		RunRecord runRecord = runservice.selectRunRecordByUidOrder(user.getId());
		runRecord.setEndDate(new java.sql.Date(new Date().getTime()));

		Propertr_user qinxuejin = new Propertr_user();
		qinxuejin.setuId(user.getId());
		qinxuejin.setpId(1);
		qinxuejin.setCount(10);
		qinxuejin.setEuId(event_user.getEuId());
		qinxuejin.setEventName("环湖跑");

		Propertr_user xinyudu = new Propertr_user();
		xinyudu.setuId(user.getId());
		xinyudu.setpId(2);
		xinyudu.setCount(100);
		xinyudu.setEuId(event_user.getEuId());
		xinyudu.setEventName("环湖跑");

		Propertr_user zhixinli = new Propertr_user();
		zhixinli.setuId(user.getId());
		zhixinli.setpId(3);
		zhixinli.setCount(100);
		zhixinli.setEuId(event_user.getEuId());
		zhixinli.setEventName("环湖跑");

		propertr_userService.insertSelective(qinxuejin);
		propertr_userService.insertSelective(xinyudu);
		propertr_userService.insertSelective(zhixinli);

	}

	@RequestMapping("getPoint")
	public boolean getPoint(@RequestParam("latitude") BigDecimal latitude,
			@RequestParam("longitude") BigDecimal longitude, @RequestParam("speed") Float speed,
			@RequestParam("runrecordId") Integer runrecordId) {
		System.out.println("runrecordId---" + runrecordId);
		boolean flag = false;
		Point p = new Point();
		p.setLatitude(latitude);
		p.setLongitude(longitude);
		p.setSpeed(speed);
		p.setRunrecordId(runrecordId);
		int i = runservice.insertSelective(p);
		if (i > 0) {
			flag = true;
		}
		return flag;
	}

	@RequestMapping("getLine")
	public List<Point> getLine(@RequestParam("runrecordId") Integer runrecordId) {

		return runservice.selectByRunRecordId(runrecordId);
	}

	@PostMapping("checkRun")
	public String checkRun(HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("user");
		
		Event_user event_user = event_userService.selectEidByUidOrder(user.getId());
		
		return event_user.getType();
	}

	@PostMapping("getRunRecord")
	public Integer getRunRecord(HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("user");

		return runservice.selectRunRecordByUidOrder(user.getId()).getId();

	}
}
